import { _decorator, Component, Label, tween, Tween, UITransform } from 'cc';
import { DataProxy } from '../../../../frame/A-LIB/sources/lib.b.data';
import GameDC from '../../../data/data.game.center';
import { Float } from '../float';
const { ccclass, property } = _decorator;

@ccclass('UIHomeTopHilit')
export class UIHomeTopHilit extends Component {
	private uitrans: UITransform;
	private vecasit: mtec.cc.VecAssist;

	private ui_diamond: UITransform;

	private hilit_diamond_token: string;

	protected onLoad(){
		this.init();
		this.listening();
	}

	protected start(){
		this.initUIStatus();
	}

	private init(){
		this.uitrans = this.node.getComponent(UITransform);
		this.vecasit = new mtec.cc.VecAssist(this.node);

		this.ui_diamond = this.node.getChildByName('diamond').getComponent(UITransform);
	}

	private initUIStatus(){
		this.vecasit.getPosition('center', 'top');
		if(mtec.cc.is_long_screen){
			this.vecasit.vec.add3f(0, -100, 0);
		}
		this.node.setPosition(this.vecasit.vec);

		this.ui_diamond.node.active = false;
	}

	private listening(){
		let diamond_obj = {value: 0};
		((obj: any, lbl: Label)=>{
			let value: number;
			Reflect.deleteProperty(obj, 'value');
			Reflect.defineProperty(obj, 'value', {
				get(){
					return value;
				},
				set(v){
					value = v;
					lbl.string = Math.round(v).toString();
				}
			});
		})(diamond_obj, this.ui_diamond.node.getChildByName('lbl-value').getComponent(Label));

		diamond_obj.value = GameDC.User.diamond;

		DataProxy.follow(GameDC.Trigger, 'HILIT_DIAMOND', (_, hilit)=>this.onHilitDiamondTrigger(hilit, _), false);
		DataProxy.follow(GameDC.Trigger, 'UIHome', (_, v)=>{
			this.vecasit.getPosition('center', 'top');
			if(mtec.cc.is_long_screen){
				this.vecasit.vec.add3f(0, -100, 0);
			}
			this.node.setPosition(this.vecasit.vec);
			this.node.active = v;
		}, false);
		DataProxy.follow(GameDC.User, 'diamond', (_, v)=>{
			Tween.stopAllByTarget(diamond_obj);
			tween(diamond_obj).to(0.8, {value: v}).start();
		}, false);
	}

	private onHilitDiamondTrigger(hilit: boolean, ov: boolean){
		this.ui_diamond.node.active = hilit;
		if(hilit){
			this.hilit_diamond_token = Float.heightLight([this.ui_diamond.node]);
		}else if(this.hilit_diamond_token){
			Float.releaseHILIT(this.hilit_diamond_token);
			this.hilit_diamond_token = undefined;
		}
	}
}

